package com.example.testapp.activity.activity.menu;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.testapp.R;
import com.example.testapp.activity.dabastate.CalendarDao;
import com.example.testapp.activity.model.CalendarVO;
import com.example.testapp.activity.util.CalendarContract;
import com.example.testapp.activity.view.BorderEditText;
import com.example.testapp.activity.view.BorderTextView;

/**
 * Created by l on 2016/6/5.
 */
public class CalendarInfoViewActivity extends AppCompatActivity {

    private LinearLayout Layouts = null;
    private BorderTextView textTop = null;
    private BorderTextView info = null;
    private BorderTextView date = null;
    private BorderTextView type = null;
    private BorderEditText editInfo = null;
    private CalendarDao   Dao = null;
    private CalendarVO  calendarVO = null;

    private String  scheduleInfo = "";    //日程信息被修改前的内容
    private String scheduleChangeInfo = "";  //日程信息被修改之后的内容
    private LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dao = new CalendarDao(this);

        params.setMargins(0, 5, 0, 0);
        Layouts = new LinearLayout(this);
        Layouts.setOrientation(LinearLayout.VERTICAL);
        Layouts.setBackgroundResource(R.mipmap.schedule_bk);
        Layouts.setLayoutParams(params);

        textTop = new BorderTextView(this, null);
        textTop.setTextColor(Color.BLACK);
        textTop.setBackgroundResource(R.mipmap.top_day);
        textTop.setText("日程详情");
        textTop.setHeight(80);
        textTop.setGravity(Gravity.CENTER);

        editInfo = new BorderEditText(CalendarInfoViewActivity.this, null);
        editInfo.setTextColor(Color.BLACK);
        editInfo.setBackgroundColor(Color.WHITE);
        editInfo.setHeight(200);
        editInfo.setGravity(Gravity.TOP);
        editInfo.setLayoutParams(params);
        editInfo.setPadding(10, 5, 10 ,5);

        Layouts.addView(textTop);

        Intent intent = getIntent();
        String[] scheduleIDs = intent.getStringArrayExtra("calendarID");
        for (int i=0; i<scheduleIDs.length; i++){
            handlerInfo(Integer.parseInt(scheduleIDs[i]));
        }
        setContentView(Layouts);
    }

    /**
     * 显示日程所有信息
     */
    private void handlerInfo(int scheduleID) {
       date = new BorderTextView(this, null);
       date.setTextColor(Color.BLACK);
       date.setBackgroundColor(Color.WHITE);
       date.setLayoutParams(params);
       date.setGravity(Gravity.CENTER_VERTICAL);
       date.setHeight(60);
       date.setPadding(10, 0, 10, 0);

        type = new BorderTextView(this, null);
        type.setTextColor(Color.BLACK);
        type.setBackgroundColor(Color.WHITE);
        type.setLayoutParams(params);
        type.setGravity(Gravity.CENTER_VERTICAL);
        type.setHeight(60);
        type.setPadding(10, 0, 10, 0);
        type.setTag(scheduleID);

        info = new BorderTextView(this, null);
        info.setTextColor(Color.BLACK);
        info.setBackgroundColor(Color.WHITE);
        info.setLayoutParams(params);
        info.setGravity(Gravity.CENTER_VERTICAL);
        info.setHeight(50);
        info.setPadding(10, 5, 10, 5);


        Layouts.addView(type);
        Layouts.addView(date);
        Layouts.addView(info);

        calendarVO = Dao.getScheduleByID(scheduleID);
        if(calendarVO != null){
            date.setText(calendarVO.getScheduleDate());
            type.setText(CalendarContract.sch_type[calendarVO.getScheduleTypeID()]);
            info.setText(calendarVO.getScheduleContent());
        }

        type.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final String scheduleID = String.valueOf(v.getTag());
                new AlertDialog.Builder(CalendarInfoViewActivity.this)
                        .setTitle("删除日程")
                        .setMessage("确认删除")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Dao.delete(Integer.parseInt(scheduleID));
                                Intent intent = new Intent();
                                intent.setClass(CalendarInfoViewActivity.this, CalendarAllActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("取消", null).show();

                return true;
            }
        });
    }
}
